from CommonVarRule import CommonVarRule
from NongLiAssistant import NongLiAssistant
import borax
from borax.calendars import LunarDate

"""
 
"""


class MingZhuInfo:
    """命主信息"""

    def __init__(self):
        self.Name = ""
        self.sexYingYang = None
        self.CompleteDateTime = ""
        self.WuXingJu = ""
        self.MingZhuXing = ""
        self.ShenZhuXing = ""
        self.SexNameString = ""
        self.YearGongLi = ""
        self.YearNongLi = ""
        self.yearGanZhi = ""
        self.monthGanZhi = ""
        self.dayGanZhi = ""
        self.hourGanZhi = ""
        self.yearTianGan = ""
        self.yearDiZhi = ""
        self.monthName = ""
        self.dayName = ""
        self.hourName = ""
        self.sex = -1  # 1 男 0 女
        self.year = 0
        self.month = 0  # 月份(-2:闰二月)
        self.day = 0
        self.hour = 0
        self.minute = 0

    @property
    def CompleteLunarDateTime(self):
        """完整农历信息"""
        return (
            f"{self.yearGanZhi}年 {self.monthName} {self.dayName}日 {self.hourName}时"
        )

    @classmethod
    def GetMingZhuXing(
        cls,
        year,
        lunarMonthName,
        lunarDayName,
        lunarHour,
        sexInfo="男",
        name="匿名",
        leapMonth="",
    ):
        """
        获取命造信息 (leapMonth eg: 闰四月则，四月)
        :param year: 农历年份
        :param lunarMonthName: 农历月份
        :param lunarDayName: 农历日
        :param lunarHour: 农历时辰
        :param sexInfo: 性别信息
        :param name: 姓名
        :param leapMonth: 闰月信息
        :return: MingZhuInfo实例
        """
        # 假设NongLiAssistant已实现
        ganzhi = NongLiAssistant.GongYunToGanZhi(int(year))

        ent = MingZhuInfo()
        ent.Name = name
        ent.hourName = lunarHour
        ent.dayName = lunarDayName
        ent.monthName = lunarMonthName
        ent.yearGanZhi = ganzhi

        if sexInfo == "男":
            sexInfo = "阳"
            ent.sex = 1
        elif sexInfo == "女":
            sexInfo = "阴"
            ent.sex = 0

        try:
            ent.YearNongLi = year
     
            ent.sexYingYang = sexInfo == "阳" if YinYangEnum.阳 else YinYangEnum.阴
            ent.yearTianGan = ganzhi[0]
            ent.yearDiZhi = ganzhi[1]

            _year = int(year)
            ent.year = _year

            # 月份数字转换
            monthNo = 0
            try:
                monthNo = int(ent.monthName)
                # 假设CommonVarRule.MONTH_No_MAP已定义
                for k, v in CommonVarRule.MONTH_No_MAP.items():
                    if v == monthNo:
                        ent.monthName = k
                        break
            except ValueError:
                monthNo = CommonVarRule.MONTH_No_MAP.get(ent.monthName, 0)

            # 时辰转换
            hourNo = 0
            try:
                hourNo = int(ent.hourName)
                if hourNo == 0:  # 晚子时
                    ent.hourName = "子"
                else:
                    # 假设NongLiAssistant.SHIER_HOUR已定义
                    found = False
                    for hour_range in NongLiAssistant.SHIER_HOUR:
                        if hour_range.MinHour <= hourNo <= hour_range.MaxHour:
                            ent.hourName = hour_range.HourName
                            found = True
                            break
                    if not found:
                        if hourNo == 23:
                            ent.hourName = "子"
                        else:
                            for k, v in CommonVarRule.DiZhi_No_MAP.items():
                                if v == hourNo:
                                    ent.hourName = k
                                    break
            except ValueError:
                hourNo = CommonVarRule.DiZhi_No_MAP.get(ent.hourName, 0)

            # 日期转换
            dayNo = 0
            try:
                dayNo = int(ent.dayName)
                # 假设CommonVarRule.SHENRI_NO_MAP已定义
                for k, v in CommonVarRule.SHENRI_NO_MAP.items():
                    if v == dayNo:
                        ent.dayName = k
                        break
            except ValueError:
                dayNo = CommonVarRule.SHENRI_NO_MAP.get(ent.dayName, 0)

            ent.day = dayNo

            # 月份处理
            _month = 0
            # 假设NongLiAssistant.Lunar_Month_Arr已定义
            if ent.monthName in NongLiAssistant.Lunar_Month_Arr:
                _month = NongLiAssistant.Lunar_Month_Arr.index(ent.monthName)

            if leapMonth:  # 闰月处理
                if leapMonth in NongLiAssistant.Lunar_Month_Arr:
                    leap_index = NongLiAssistant.Lunar_Month_Arr.index(leapMonth)
                    _month = -leap_index

            if ent.monthName == "腊月":
                _month = 12
            elif ent.monthName == "一月":
                _month = 1

            ent.month = _month

            # 时辰转换
            # 假设NongLiAssistant.LunarHourTo返回(时辰名称, 整数值)
            hour_res = NongLiAssistant.LunarHourTo(ent.hourName)
            ent.hour = hour_res[1]  # 取中间值

            gakki_birth = LunarDate(_year, _month, dayNo)
            ent.monthGanZhi = gakki_birth.gz_month

        except Exception as e:
            # 实际使用时建议记录错误
            raise e  # 或者使用日志记录错误信息

        return ent

    @staticmethod
    def GetDefault():
        """获取默认命造信息"""
        ent = MingZhuInfo()
        ent.Name = "w"
        ent.hourName = "辰"
        ent.dayName = "初一"
        ent.monthName = "正月"
        ent.sexYingYang = YinYangEnum.阳
        ent.yearGanZhi = "壬申"
        ent.yearDiZhi = "申"
        ent.yearTianGan = "壬"
        return ent

    @staticmethod
    def SetSexName(mingzao):
        """设置性别名称(阳男)"""
        arr = CommonVarRule.CONST_TIANGAN  # 假设已定义
        gIdx = 1
        for item in arr:
            if mingzao.yearTianGan == item:
                break
            gIdx += 1

        isEven = gIdx % 2 == 0  # 偶数
        if isEven and mingzao.sexYingYang == YinYangEnum.阳:
            mingzao.SexNameString = "阴男"
        elif not isEven and mingzao.sexYingYang == YinYangEnum.阳:
            mingzao.SexNameString = "阳男"
        elif not isEven and mingzao.sexYingYang == YinYangEnum.阴:
            mingzao.SexNameString = "阳女"
        elif isEven and mingzao.sexYingYang == YinYangEnum.阴:
            mingzao.SexNameString = "阴女"


# 枚举类示例 (需单独定义)
class YinYangEnum:
    阳 = 1
    阴 = 0


if __name__ == "__main__":
    # 测试代码
    # test1= MingZhuInfo.GetDefault()
    # print(MingZhuInfo.GetMingZhuInfo(test1))
    mingzao = MingZhuInfo().GetMingZhuXing("1990", "5", "4", "卯", "男", "张三")
    print(mingzao)
